﻿using System;
using System.Web;

namespace ASP.NET_MVC_Static_Test.Common
{
    public static class ClientHelper
    {
        /// <summary>
        /// 获取用户IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetClientIp()
        {
            try
            {                
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                string[] arr = (result + "").Trim().Split(',');
                result = arr[0];
                return result;
            }
            catch (Exception ex)
            {
                //
            }
            return string.Empty;
        }
    }
}